<?php
class Module
{
	const INITIALIZE_MODULE = "<Module>: Initialize";
	const UNLOAD_MODULE = "<Module>: Unload";
	
	public $name;
	public $function;
	
	protected $initialized = false;
	
	function __construct($aName)
	{
		$this->name = $aName;
	}
	
	public function startLoadProgress()
	{
		$modulePath = PATHS::modules() . $this->name . ".php";
		DEBUG::log("Starting load progress on module " . $this->name, Debug::DEBUG);
		
		if (is_file($modulePath) && is_readable($modulePath))
		{
			$fileContents = file_get_contents($modulePath);
			$fileContents = str_replace("<?php","",$fileContents);
			$fileContents = str_replace("?>","",$fileContents);
			
			DEBUG::log("Creating new internal module function for " . $this->name, Debug::DEBUG);
			
			$dummyFunction = create_function('$aEvent', $fileContents);
			
			if ($dummyFunction != null)
			{
				$this->function = $dummyFunction;
				DEBUG::log("Successfully created internal module function for " . $this->name, Debug::DEBUG);
			}
		}
		else
		{
			DEBUG::log("Module file (" . $modulePath . ") is not readable or has not been found.", Debug::ERROR);
		}
	}
	
	public function initialize()
	{
		if ($this->function == null)
		{
			DEBUG::log("Module " . $this->name . " hasn't loaded yet", Debug::ERROR);
			return;
		}
		
		if ($this->initialized)
		{
			DEBUG::log("Module " . $this->name . " has already been initialized", Debug::WARNING);
			return;
		}

		$this->run(new Event(Module::INITIALIZE_MODULE));
	}
	
	public function run($aEvent)
	{
		call_user_func($this->function, $aEvent);
	}
}
?>